package com.example.week9_advanceui;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.week9_advanceui.R.id.parent;

public class FileBrowse_Activity extends AppCompatActivity {

    //定义组件
    ListView listView;
    TextView textView;

    //记录当前的父文件夹
    File currentParent;
    //记录当前路径下所有文件的文件数组
    File[] currentFiles;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_browse_);


        //初始化组件：获取1列出全部文件的ListView
        listView = (ListView) findViewById(R.id.fileList);
        textView = (TextView) findViewById(R.id.filePath);
        //fileParent= (Button) findViewById(R.id.fileParent);
        //获取系统的SD卡目录
        File root = Environment.getExternalStorageDirectory();
        //如果SD卡存在
        if (root.exists()) {
            //当前目录为SD卡的根目录
            currentParent = root;
            currentFiles = root.listFiles();
            //调用自定义函数，用当前目录下的全部文件、文件夹来填充ListView
            inflateListView(currentFiles);
        }

        //为ListView的列表项的单机事件绑定监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (currentFiles[position].isFile())
                    return;

                File[] tmp = currentFiles[position].listFiles();

                if (tmp == null || tmp.length == 0) {//若该文件夹为空
                    Toast.makeText(FileBrowse_Activity.this, "当前路径不可访问或该路径下没有文件", Toast.LENGTH_LONG).show();
                }
                else
                {
                    currentParent = currentFiles[position];
                    currentFiles = tmp;
                    inflateListView(currentFiles);
                }

                Button parent = (Button) findViewById(R.id.fileParent);

                parent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            if (!currentParent.getCanonicalPath().equals("/storage/emulated/0")) {
                                currentParent = currentParent.getParentFile();

                                currentFiles = currentParent.listFiles();

                                inflateListView(currentFiles);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

            private void inflateListView(File[] files) {
                //创建一个List集合，用作为适配器中的数据源。List集合的元素的Map。
                List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
                //组装将用于适配器数据源的List集合
                for (int i = 0; i < files.length; i++) {
                    //新建一个Map对象
                    Map<String, Object> listItem = new HashMap<>();
                    //为新建的Map对象置初值
                    // 如果当前File是文件夹，使用folder图标；否则使用file图标
                    if (files[i].isDirectory()) {
                        listItem.put("icon", R.drawable.folder);
                    } else {
                        listItem.put("icon", R.drawable.file);
                    }
                    //记录文件夹或文件的名字
                    listItem.put("fileName", files[i].getName());//将map作为list项添加到List集合中
                    listItems.add(listItem);
                }

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,R.layout.fileitem, new String[]{"icon", "fileName"}, new int[]{ R.id.fileIcon, R.id.file_name});


                //为ListView设置Adapter
                listView.setAdapter(simpleAdapter);

                //显示当前路径
                try {
                    textView.setText("当前路径为：" + currentParent.getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }


